package com.mingqi.enetconnected.data.source.local;


import android.content.Context;
import android.database.sqlite.SQLiteDatabase;

import com.j256.ormlite.android.apptools.OrmLiteSqliteOpenHelper;
import com.j256.ormlite.support.ConnectionSource;
import com.j256.ormlite.table.TableUtils;
import com.mingqi.enetconnected.base.MyApplication;
import com.mingqi.enetconnected.data.bean.order.OrderBrief;
import com.mingqi.enetconnected.util.log.Log2FileUtils;

/**
 * Ormlite数据库初始化
 *
 */

public class DBHelper extends OrmLiteSqliteOpenHelper {
    private final String TAG= "DBHelper";

    /**
     * 定义数据库路径,库文件名,为便于调试,暂时放在SDCard/towngasCS下,发布时将路径去掉,仅保留文件名
     */
    public static String DATABASE_NAME = MyApplication.projDir+"/towngascs.db";

    /**
     * 定义数据库版本
     */
    private static final int DATABASE_VERSION = 1;
    public DBHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase sqLiteDatabase, ConnectionSource connectionSource) {
        Log2FileUtils.e(TAG,"onCreate");

        createTableByOrm(connectionSource);
    }

    @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, ConnectionSource connectionSource, int i, int i1) {
        Log2FileUtils.e(TAG,"onUpgrade");
    }

    /**
     * 建表
     * */
    private void createTableByOrm(ConnectionSource connectionSource){
        try{
            for(Class<?> cls :arrDynCls){
                TableUtils.createTable(connectionSource, cls);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    /**
     * 凡是orm定义的表,都加在此数据中
     * */
    private static final Class<?>[] arrDynCls = {
            OrderBrief.class
    };
}
